package lianxi5.Runtime;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Myframe extends JFrame implements ActionListener {
    JButton yesBut = new JButton("帅爆了");
    JButton noBut = new JButton("不帅");
    JButton midBut = new JButton("一般般");
    JLabel label = new JLabel("你觉得自己帅吗?");
    boolean isYes = false;

    public Myframe() {
        initjFrame();
        iniView();
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if (obj == yesBut) {
            JOptionPane.showMessageDialog(this, "你真有自信！");
        } else if (obj == noBut) {
            JOptionPane.showMessageDialog(this, "要相信自己的魅力哦！");
        } else if (obj == midBut) {
            JOptionPane.showMessageDialog(this, "很客观嘛！");
        }
    }

    private void initjFrame() {
        this.setTitle("帅不帅测试");
        this.setSize(300, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void iniView() {
        this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
        // 设置标签字体和颜色
        label.setFont(new Font("Arial", Font.PLAIN, 16));
        label.setForeground(Color.BLUE);
        this.add(label);
        yesBut.addActionListener(this);
        noBut.addActionListener(this);
        midBut.addActionListener(this);
        // 创建一个面板用于放置按钮，使其水平排列
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
        // 设置按钮字体和颜色
        yesBut.setFont(new Font("Arial", Font.PLAIN, 14));
        yesBut.setForeground(Color.GREEN);
        noBut.setFont(new Font("Arial", Font.PLAIN, 14));
        noBut.setForeground(Color.RED);
        midBut.setFont(new Font("Arial", Font.PLAIN, 14));
        midBut.setForeground(Color.ORANGE);
        buttonPanel.add(yesBut);
        buttonPanel.add(noBut);
        buttonPanel.add(midBut);
        this.add(buttonPanel);

        // 添加鼠标悬停效果
        yesBut.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                yesBut.setBackground(Color.LIGHT_GRAY);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                yesBut.setBackground(null);
            }
        });
        noBut.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                noBut.setBackground(Color.LIGHT_GRAY);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                noBut.setBackground(null);
            }
        });
        midBut.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                midBut.setBackground(Color.LIGHT_GRAY);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                midBut.setBackground(null);
            }
        });
    }
}
